#include "exitbutton.h"
#include "titlebar.h"
#include "dialog.h"
#include "graph.h"

widget_t *create_dialog(char* text, uint32_t width, uint32_t height)
{
    // create widget
    widget_t *self = create_widget(NULL,-1,-1,width,height,TRUE);
    // create the exit button
    widget_t *exitbtn = create_exit_button(self, width-21, 3, 18, 18);
    // connect the CLICKED signal to call dialog_slot_exit function
    CONNECT(exitbtn, CLICKED, self, dialog_slot_exit);
    // create the tile bar
    create_title_bar(self, 3, 3, width-26, 18);
    // set the application name
    dialog_set_text(self, text);
    // set the callback functions
    callback_t *callback = self->callback;
    callback->paint = dialog_paint;
    callback->key_down = dialog_key_down;
    // return a pointer to the widget
    return self;
}

void dialog_set_text(widget_t *self, char *text)
{
    self->text = text;
    SendMessage(self->hwnd, WM_SETTEXT, (WPARAM)0, (LPARAM)text);
}

void dialog_paint(widget_t *self, HDC hdc)
{
    RECT rect;
    GetClientRect(self->hwnd, &rect);

    // fill the background
    rgb_t top = {220,220,220};
    rgb_t bottom = {140,140,140};
    horz_gradient_rect(self, &rect, top, bottom);

    // draw the edge
    rgb_t upper_edge = {230, 230, 230};
    rgb_t lower_edge = {100, 100, 100};
    line(self, 0, 0, rect.right-1, 0, upper_edge);
    line(self, 0, 0, 0, rect.bottom-1, upper_edge);
    line(self, rect.right-1, 0, rect.right-1, rect.bottom-1, lower_edge);
    line(self, 0, rect.bottom-1, rect.right-1, rect.bottom-1, lower_edge);
}

void dialog_key_down(widget_t *self, uint32_t vkey)
{
    // quit if the user press ESCAPE
    if(vkey == VK_ESCAPE)
    {
        PostMessage(self->hwnd, WM_CLOSE, (WPARAM)NULL, (LPARAM)NULL);
    }
}

void dialog_slot_exit(widget_t *self, void *param)
{
    PostMessage(self->hwnd, WM_CLOSE, (WPARAM)NULL, (LPARAM)NULL);
}

